package models

import (
	"gorm.io/gorm"
	"worklog/utils"
)

type User struct {
	gorm.Model
	UserName    string `json:"username" ,valid:"required,length(1|15)"`
	Salt        string `json:"salt"`
	PassWord    string `json:"password" ,valid:"required,length(8|16)"`
	MD5Password string `json:"md5_password"`
}

func (user User) TableName() string {
	return "user"
}

func GetUser() []*User {
	data := make([]*User, 0)
	utils.DB.Select("id", "user_name", "md5_password").Find(&data)
	return data
}
func CreateUser(user User) error {
	utils.DB.Create(&user)
	return nil
}
func FindUserBYUsername(username string) User {
	user := User{}
	utils.DB.Where("user_name = ?", username).First(&user)
	return user
}
func GetPasswordByUsername(username string) string {
	user := User{}
	utils.DB.Where("user_name = ?", username).First(&user)
	return user.PassWord
}
